package com.translate.web.service.impl;

import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.ExampleMapper;
import com.translate.entity.Example;
import com.translate.vo.contract.IExampleVO;
import com.translate.web.service.IExampleService;
import com.basic.service.impl.BaseServiceImpl;
import com.param.contract.IPage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class ExampleServiceImpl extends BaseTranslateServiceImpl<Example> implements IExampleService {
	
	public ExampleServiceImpl(ExampleMapper mapper) {
		super(mapper);
	}

	@Override
	public <VO extends IExampleVO> List<VO> getListFromCache(Class<VO> target) {

		return this.entityConvertToVo(this.listToCache(
				RedisConstant.EXAMPLE_DATA_KEY, 
				8), target);
		
	}
	
	protected <S extends IPage> String sqlByGetCounttAfterProcesser(S param) {
		return this.sqlByGetCount(" enabled = 1 and del = 0 ");
	}
	
	protected <S extends IPage> String sqlByGetPageListAfterProcesser(S param) {
		return this.sqlByGetPageList(" enabled = 1 and del = 0");
	}
	
	
	protected String sqlByGetPageList(String condition) {

		String sql = getNoConditionSql();


		if (!condition.equals("")) {

			sql += "where " + condition;
		}

		sql += " order by sort asc limit ?,?  ";

		return sql;
	}
	
	
	
	/**
	 * 分页查询
	 * 
	 * @param <S>
	 * @param param
	 * @param statement
	 * @throws SQLException
	 */
	protected <S> void bindParam(S param, PreparedStatement statement) throws SQLException {

		IPage pageParam = (IPage) param;
		
		pageParam.setSelectCount(8);

		statement.setInt(1, pageParam.getStart());

		statement.setInt(2, pageParam.getSelectCount());
	}
	
	
	@Resource
	public void setRedisTemplate(RedisTemplate<String, List<Example>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	protected Class<Example> getEntityClass() {
		return Example.class;
	}
}